package com.customview.custommPath;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/10/21 16:42
 */
// TODO: 2019/10/21 path 的路线
public class CustomPath extends View {
    public CustomPath(Context context) {
        this(context, null);
    }

    public CustomPath(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomPath(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    Path mPath;
    Path mSPath;
    Paint mPaint;
    Float animatedValue = 0f;
    PathMeasure measure;
    float length;

    private void init() {

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(5);
        mPath = new Path();
        mSPath = new Path();

        /**
         * 自定义路径
         * close 是会实现闭合的
         */

//        customPathinit();


        /**
         * 画圆
         */

//        initcircle();


        //forceClosed 闭合
//        measure = new PathMeasure(mPath, false);
//        length = measure.getLength();

//        animateinit();


    }

    private void animateinit() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                Log.i("test", "onAnimationUpdate: " + ((float) animation.getAnimatedValue()));
                animatedValue = (Float) animation.getAnimatedValue();

                postInvalidate();
            }
        });
        valueAnimator.setDuration(5000);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("onMeasure", "onMeasure: " + getMeasuredWidth());
        Log.i("onMeasure", "onMeasure: " + getMeasuredHeight());
        customPathinit();

//        animateinit();
    }

    int phase=0;
    private void customPathinit() {
        mPath.moveTo(getMeasuredWidth() / 2, 80);
        mPath.lineTo(getMeasuredWidth() / 2 - 80, 20);
        mPath.lineTo(getMeasuredWidth() / 2 - 100, 30);
        mPath.lineTo(getMeasuredWidth() / 2 - 120, 40);
        mPath.lineTo(getMeasuredWidth() / 2 - 180, 60);
        mPath.lineTo(getMeasuredWidth() / 2, 160);
        measure = new PathMeasure(mPath, false);
        length = measure.getLength();
        /**
         * new CornerPathEffect(50); 在自定义path 线时候 可以让转折角变得圆角
         * CornerPathEffect的构造方法只接受一个参数radius，意思就是转角处的圆滑程度。
         */
//        CornerPathEffect  mEffects = new CornerPathEffect(50);

        /**DiscretePathEffect(8.0F, 10.0F)
         * 第一个呢指定这些突出的“杂点”的密度，值越小杂点越密集；（好比绳子的褶皱程度，越小就越褶皱）
         *
         * 第二个参数呢则是“杂点”突出的大小，值越大突出的距离越大反之反之。(好比绳子的褶皱角突出的幅度， 值越大的突出越大)
         */
        DiscretePathEffect mEffects = new DiscretePathEffect(8.0F, 10.0F);


        /**
         * 虚实线结合效果
         *
         *第一个参数是一个浮点型的数组，那这个数组有什么意义呢？其实是这样的，
         * 我们在定义该参数的时候只要浮点型数组中元素个数大于等于2 即可，
         *
         * DashPathEffect的第二个参数（phase）我称之为偏移值，
         * 动态改变其值会让路径产生动画的效果。
         */



//        DashPathEffect mEffects =new DashPathEffect(new float[]{8,5},phase);

        mPaint.setPathEffect(mEffects);



/*        final Handler handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                phase++;
                DashPathEffect mEffects =new DashPathEffect(new float[]{8,5},phase);
                mPaint.setPathEffect(mEffects);
                postInvalidate();
                handler.post(this);
            }
        },1000);*/


    }

    private void initcircle() {
        mPath.addCircle(100, 100, 100, Path.Direction.CW);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //自定义路线
        customPath(canvas);

        //圆的慢慢画出
//        circle(canvas);

        //在圆的最后点上
//        runOnCircle(canvas);

    }

    private void customPath(Canvas canvas) {
        mSPath.reset();
//        float v = length * animatedValue;
        float v = length * 1;
        boolean segment = measure.getSegment(0, v, mSPath, true);
        canvas.drawPath(mSPath, mPaint);

    }

    private void runOnCircle(Canvas canvas) {
        //清理之前的数据
        float v = length * animatedValue;
        float[] pos = new float[2];
        float[] tan = new float[2];
        measure.getPosTan(v, pos, tan);
        canvas.drawCircle(pos[0], pos[1], 50, mPaint);

    }

    private void circle(Canvas canvas) {
        //清理之前的数据
        mSPath.reset();
        float v = length * animatedValue;
        boolean segment = measure.getSegment(0, v, mSPath, true);
        canvas.drawPath(mSPath, mPaint);


    }
}
